package com.cgtong.android.activity;

import android.app.Dialog;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.cgtong.android.App;
import com.cgtong.android.R;
import com.cgtong.android.base.BaseTitleActivity;
import com.cgtong.android.bean.UserInfoBean;
import com.cgtong.android.db.UserAccount;
import com.cgtong.android.event.GetUserEvent;
import com.cgtong.android.mvp.contract.GetUserContract;
import com.cgtong.android.mvp.presenter.GetUserPresenter;
import com.cgtong.android.util.CommonUtils;
import com.cgtong.android.util.PostUtil;
import com.cgtong.android.widget.BaseDialog;
import com.kingja.loadsir.callback.SuccessCallback;

import org.simple.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

public class MyInfoActivity extends BaseTitleActivity implements GetUserContract.View {

    private static final int REQ_AVATAR = 100;

    //    @BindView(R.id.title_center_txt)
//    TextView title_center_txt;
    @BindView(R.id.et_sex)
    TextView et_sex;
    @BindView(R.id.et_rlname)
    EditText et_rlname;
    @BindView(R.id.et_nickname)
    EditText et_nickname;
    @BindView(R.id.et_id)
    EditText et_id;
    @BindView(R.id.et_phone)
    TextView et_phone;
    @BindView(R.id.avatar_img)
    CircleImageView avatar_img;
    @BindView(R.id.et_location)
    EditText locationEdt;

    private UserAccount account;
    private GetUserPresenter presenter;
    private int avasterid;
    private String mSex;
    private String page_from;

    @Override
    protected String getContentTitle() {
        return getString(R.string.txt_myinfo_title);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_info;
    }

    @Override
    protected void loadDataByNet() {
        initView();
        if (getIntent() != null) {
            page_from = getIntent().getStringExtra("from");
            if (RegisterActivity.REGISTER_PAGE_ID.equals(page_from)) {
                mTitleLeftTxt.setVisibility(View.GONE);
            }
        }
        PostUtil.postCallbackDelayed(loadService, SuccessCallback.class);
    }

    @Override
    protected void onDataReloadByNet(View v) {

    }

    private void initView() {
        account = App.getInstance().getCurrentAccount();
        presenter = new GetUserPresenter(this);

        if (account != null) {
            avasterid = account.getAvatarId();
            mSex = account.getSex() + "";
            et_rlname.setText(account.getName());
            et_nickname.setText(account.getNickName());
            et_id.setText(account.getIdc());
            et_sex.setText(account.getSex() == 1 ? "男" : "女");
            et_nickname.setText(account.getNickName());
            et_phone.setText(account.getPhone());
            locationEdt.setText(account.getAddress());
            CommonUtils.setUserAvatar(this, avatar_img, avasterid);
        }
    }

    @Override
    public void getUserInfo(UserInfoBean.UserInfoBeanObj accountObj) {
    }

    @Override
    public void setUserInfoSuc(String msg) {
        ToastUtils.showShort(msg);
        if (RegisterActivity.REGISTER_PAGE_ID.equals(page_from)) {
            try {
                if (account == null) {
                    account = App.getInstance().getCurrentAccount();
                }
                if (null != account) {
                    String rlname = et_rlname.getText().toString();
                    String nickname = et_nickname.getText().toString();
                    String idc = et_id.getText().toString();
                    account.setIdc(StringUtils.isEmpty(idc) ? "" : idc);
                    account.setName(StringUtils.isEmpty(rlname) ? "" : rlname);
                    account.setNickName(StringUtils.isEmpty(nickname) ? "" : nickname);
                    account.setAvatarId(avasterid);
                    account.setSex(Integer.parseInt(mSex));
                    App.getInstance().updateAccount(account);
                }
            } catch (Exception e) {
            }
            Intent intent = new Intent(MyInfoActivity.this, MainActivity.class);
            MyInfoActivity.this.startActivity(intent);
        } else {
            EventBus.getDefault().post(new GetUserEvent());
        }
        finish();
    }

    @Override
    public void onBackPressed() {
        if (RegisterActivity.REGISTER_PAGE_ID.equals(page_from)) {
            Intent intent = new Intent(MyInfoActivity.this, MainActivity.class);
            MyInfoActivity.this.startActivity(intent);
        }
        finish();
    }

    @Override
    public void setUserInfoFail(String msg) {
        ToastUtils.showShort(msg);
    }

    /**
     * 性别选择
     */
    private void showSexDialog() {
        BaseDialog.create(getSupportFragmentManager())
                .setLayoutRes(R.layout.layout_whether_sex)
                .setViewListener(new BaseDialog.ViewListener() {

                    @Override
                    public void bindView(View view, final Dialog dialog) {
                        view.findViewById(R.id.male_txt).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mSex = "1";
                                et_sex.setText(getString(R.string.txt_sex_male));
                                dialog.cancel();
                            }
                        });
                        view.findViewById(R.id.female_txt).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mSex = "2";
                                et_sex.setText(getString(R.string.txt_sex_female));
                                dialog.cancel();
                            }
                        });
                        view.findViewById(R.id.cancel_txt).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.cancel();
                            }
                        });


                    }
                }).setDimAmount(0.3f)
                .setCanCancelable(false).
                setGravity(Gravity.BOTTOM)
                .show();
    }

    private void commit() {
        if (account != null) {
            String rlname = et_rlname.getText().toString();
            String nickname = et_nickname.getText().toString();
            if (RegisterActivity.REGISTER_PAGE_ID.equals(page_from)) {
                if (TextUtils.isEmpty(rlname)) {
                    ToastUtils.showShort(R.string.txt_empty_real_name);
                    return;
                }
                if (TextUtils.isEmpty(nickname)) {
                    ToastUtils.showShort(R.string.txt_empty_nick_name);
                    return;
                }
            }
            String id = et_id.getText().toString();
            String location = locationEdt.getText().toString();
            presenter.setUserInfo(rlname, nickname, id, avasterid + "", account.getPhone(), mSex,location);
        }
    }

    @OnClick({R.id.title_left_txt, R.id.layout_avatar, R.id.layout_sex, R.id.txt_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_txt:
                finish();
                break;
            case R.id.layout_avatar://头像
                startActivityForResult(new Intent(this, MyAvatarActivity.class), REQ_AVATAR);
                break;
            case R.id.layout_sex://性别
                showSexDialog();
                break;
            case R.id.txt_save:
                commit();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQ_AVATAR && resultCode == RESULT_OK) {
            avasterid = data.getIntExtra("avasterid", 0);
            CommonUtils.setUserAvatar(this, avatar_img, avasterid);
        }
    }
}
